package com.zmn.oms.task.job.third.core;

import com.alibaba.fastjson.JSON;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.model.entity.chan.FacadeMessageEvent;
import com.zmn.oms.services.interfaces.chan.FacadeSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 类描述：重发渠道接口消息事件任务
 *
 * @author LiangHailong
 * @date 2020-12-07 23:38:50
 */
@LTS
@Slf4j
public class FacadeMessageEventResendJob {
    @Autowired
    private FacadeSyncService facadeSyncService;

    /**
     * 重发渠道接口消息事件
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_RESEND_FACADE_MSG_EVENT_JOB)
    public Result runWorkJob(Job job) {
        String eventStr = job.getParam("event");
        log.info("[FacadeMessageEventResendJob]: FacadeMessageEvent=[{}]", eventStr);

        try {
            FacadeMessageEvent facadeMessageEvent = JSON.parseObject(eventStr, FacadeMessageEvent.class);
            facadeSyncService.sendFacadeMessageEventSync(facadeMessageEvent);
            return new Result(Action.EXECUTE_SUCCESS, "Execute send FacadeMessageEvent job success!");
        } catch (Exception e) {
            log.error(String.format("[FacadeMessageEventResendJob]: Execute job fail: %s, FacadeMessageEvent=[%s]",
                    e.getMessage(), eventStr), e);
            return new Result(Action.EXECUTE_FAILED, e.getMessage());
        }
    }
}
